
***********************************************
* Rwanda provider study
***********************************************

***********************************************
* Contents
***********************************************

	* (0.0) Set up
	* (0.1) Grab variables
	* (0.2) Define indicators
	
***********************************************
* (0.0) Set up
***********************************************
	
	clear all
	clear matrix
	set mem 250m
	set matsize 1000
	set more off
	tempfile temp1
	pause on
    eststo clear
	
	cd "C:\Users\Diana\Dropbox\RWA_P4P\SPA\do_files"
	* Define file locals
	do rwanda_global_define.do

    cap log close
    log using `"$d_out\logs\logresults_`c(current_date)'.txt"', text replace
	
***********************************************
* (0.1) Define basic variables 
***********************************************

* Providers present on day of interview
	use "$f_inventory", clear
	keep facil plnno* pulno* punit* pqual* artp* vctp* txhvp* txop* ancp* clntp* labp* pint*
	
	merge 1:1 facil using "$r_fac_controls", nogen
	
	reshape long plnno_ pulno_ punit_ pqual_ artp_ vctp_ txhvp_ txop_ ancp_ clntp_ labp_ pint_, i(facil) j(provider) string
	
	gen staff=plnno!=.
	gen doc=inlist(pqual, 1,2,3,4,5) if !mi(pqual)
	gen nurse=inlist(pqual, 8,9,10) if !mi(pqual)
		
	foreach var in doc nurse artp vctp txhvp txop ancp clntp labp staff {
		bysort facil: egen f`var'=total(`var')
		gen f`var'pc=f`var'/catchment*10000
	}

	
	keep facil fdoc fnurse fartp* fvctp* ftxhvp* ftxop* fancp* fclntp* flabp* fstaff
	bysort facil: keep if _n==1
	
	
	global fartp_desc "No. staff providing ARTs (today)"
	global fvctp_desc "No. staff providing VCTs (today)"
	global ftxhvp_desc "No. staff providing tx for HIV related illness (today)"
	global ftxop_desc "No. staff providing tx for mal/STI/TB (today)"
	global fancp_desc "No. staff providing ANC/FP/delivery (today)"
	global fclntp_desc "No. staff providing other services (today)"
	global flabp_desc "No. staff conducting lab tests (today)"

	global fartppc_desc "No. staff providing ARTs (today)/10,000 people"
	global fvctppc_desc "No. staff providing VCTs (today)/10,000 people"
	global ftxhvppc_desc "No. staff providing tx for HIV related illness (today)/10,000 people"
	global ftxoppc_desc "No. staff providing tx for mal/STI/TB (today)/10,000 people"
	global fancppc_desc "No. maternity-related staff present today/10,000 people"
	global fclntppc_desc "No. staff providing other services (today)/10,000 people"
	global flabppc_desc "No. staff conducting lab tests (today)/10,000 people"
	
	gen allstaffppc=fartppc+fvctppc+ftxhvppc+ftxoppc+fancppc+fclntppc+flabppc
	global allstaffppc_desc "No. staff present today/10,000 people"
	
	foreach var in fartp fvctp ftxhvp ftxop fancp fclntp flabp {
		lab var `var' "$`var'_desc"
		lab var `var'pc "$`var'pc_desc"
	}
	
	save `temp1', replace
	
* All providers	
	use "$f_inventory", clear
	
	keep facil a107* b107* c107* d107* q191 q192
	
	merge 1:1 facil using "$r_fac_controls", nogen
	
	egen astaff=rowtotal(a107* b107*)
	egen adoc=rowtotal(a107_01 a107_02 a107_03 a107_04 a107_05 b107_01 b107_02 b107_03 b107_04 b107_05)
	egen anurse=rowtotal(a107_08 a107_09 a107_10 b107_08 b107_09 b107_10)
	egen amidwife=rowtotal(a107_07 b107_07)
	egen amedoff=rowtotal(a107_06 b107_06)
	egen ahealthass=rowtotal(a107_11 b107_11)
	egen atech=rowtotal(a107_12 a107_13 a107_14 b107_12 b107_13 b107_14)
	egen anutr=rowtotal(a107_15 a107_16 b107_15 b107_16)
	egen asoc=rowtotal(a107_17 a107_18 a107_19 b107_17 b107_18 b107_19)
	egen apharm=rowtotal(a107_20 a107_21 b107_20 b107_21)
	egen aspec=rowtotal(a107_22 a107_23 a107_24 a107_25 a107_26 b107_22 b107_23 b107_24 b107_25 b107_26) 
	egen amanage=rowtotal(a107_27 b107_27)
	egen atechsup=rowtotal(a107_28 b107_28)
	egen amansup=rowtotal(a107_29 b107_29)
	egen aothermed=rowtotal(a107_30 b107_30)
	egen aothersup=rowtotal(a107_31 b107_31)
		
	egen mstaff=rowtotal(a107*)
	egen mdoc=rowtotal(a107_01 a107_02 a107_03 a107_04 a107_05)
	egen mnurse=rowtotal(a107_08 a107_09 a107_10)
	egen mmidwife=rowtotal(a107_07)
	egen mmedoff=rowtotal(a107_06)
	egen mhealthass=rowtotal(a107_11)
	egen mtech=rowtotal(a107_12 a107_13 a107_14)
	egen mnutr=rowtotal(a107_15 a107_16)
	egen msoc=rowtotal(a107_17 a107_18 a107_19)
	egen mpharm=rowtotal(a107_20 a107_21)
	egen mspec=rowtotal(a107_22 a107_23 a107_24 a107_25 a107_26) 
	egen mmanage=rowtotal(a107_27)
	egen mtechsup=rowtotal(a107_28)
	egen mmansup=rowtotal(a107_29)
	egen mothermed=rowtotal(a107_30)
	egen mothersup=rowtotal(a107_31)
	
	egen fstaff=rowtotal(b107*)
	egen fdoc=rowtotal(b107_01 b107_02 b107_03 b107_04 b107_05)
	
	
	egen fnurse=rowtotal(b107_08 b107_09 b107_10)
	egen fmidwife=rowtotal(b107_07)
	egen fmedoff=rowtotal(b107_06)
	egen fhealthass=rowtotal(b107_11)
	egen ftech=rowtotal(b107_12 b107_13 b107_14)
	egen fnutr=rowtotal(b107_15 b107_16)
	egen fsoc=rowtotal(b107_17 b107_18 b107_19)
	egen fpharm=rowtotal(b107_20 b107_21)
	egen fspec=rowtotal(b107_22 b107_23 b107_24 b107_25 b107_26) 
	egen fmanage=rowtotal(b107_27)
	egen ftechsup=rowtotal(b107_28)
	egen fmansup=rowtotal(b107_29)
	egen fothermed=rowtotal(b107_30)
	egen fothersup=rowtotal(b107_31)
	
	egen pstaff=rowtotal(c107*)
	egen pdoc=rowtotal(c107_01 c107_02 c107_03 c107_04 c107_05)
	egen pnurse=rowtotal(c107_08 c107_09 c107_10)
	egen pmidwife=rowtotal(c107_07)
	egen pmedoff=rowtotal(c107_06)
	egen phealthass=rowtotal(c107_11)
	egen ptech=rowtotal(c107_12 c107_13 c107_14)
	egen pnutr=rowtotal(c107_15 c107_16)
	egen psoc=rowtotal(c107_17 c107_18 c107_19)
	egen ppharm=rowtotal(c107_20 c107_21)
	egen pspec=rowtotal(c107_22 c107_23 c107_24 c107_25 c107_26) 
	egen pmanage=rowtotal(c107_27)
	egen ptechsup=rowtotal(c107_28)
	egen pmansup=rowtotal(c107_29)
	egen pothermed=rowtotal(c107_30)
	egen pothersup=rowtotal(c107_31)
	
	gen chw_any=(q191==1) if !mi(q191)
	gen chw_fp=regexm(q192,"A") if !inlist(q192, " ", "")
	gen chw_mat=regexm(q192,"B") if !inlist(q192, " ", "")
	gen chw_del=regexm(q192,"C") if !inlist(q192, " ", "")
	gen chw_child=regexm(q192,"D") if !inlist(q192, " ", "")
	gen chw_imm=regexm(q192,"E") if !inlist(q192, " ", "")
	gen chw_sti=regexm(q192,"F") if !inlist(q192, " ", "")
	gen chw_mal=regexm(q192,"G") if !inlist(q192, " ", "")
	gen chw_tb=regexm(q192,"H") if !inlist(q192, " ", "")
	gen chw_pmtct=regexm(q192,"I") if !inlist(q192, " ", "")
	gen chw_vct=regexm(q192,"J") if !inlist(q192, " ", "")
	gen chw_art=regexm(q192,"K") if !inlist(q192, " ", "")
	
	foreach kind in fp mat del child imm sti mal tb pmtct vct art {
		replace chw_`kind'=0 if q191==2
	}
	
	local staff_list1="staff doc nurse midwife medoff healthass tech nutr"
	local staff_list2="soc pharm spec manage techsup mansup othermed othersup"
	
	foreach var in `staff_list1' `staff_list2' {
		gen a`var'pc=a`var'/catchment*10000
		gen m`var'pc=m`var'/catchment*10000
		gen f`var'pc=f`var'/catchment*10000
		gen p`var'pc=p`var'/catchment*10000
		gen fte_`var'pc=a`var'pc-p`var'pc
	}
	
	*** Note: fte (particularly for doctors) is sometimes negative, if reported total is less than part-time total
	
	gen adocnursepc=adocpc+anursepc
	gen staff_target = astaffpc>=23
	global staff_target_desc "Meets staff target (23 or more/10,000 people)"
	
	local pc_list1=""
	local pc_list2=""
	
	foreach num in 1 2 {
		foreach pfix in a m f p fte_ {
			local `pfix'pc_list`num'=""
			local s_list`num'=""
				
			foreach var in `staff_list`num'' {
					local s_list`num'="`s_list`num'' a`var'"
			}
			
			foreach var in `staff_list`num'' {				
				local `pfix'pc_list`num'="``pfix'pc_list`num'' `pfix'`var'pc"
			}
		}
	}
	
	
	keep facil `s_list1' `s_list2' `apc_list1' `apc_list2' `mpc_list1' `mpc_list2' /*
		*/ `fpc_list1' `fpc_list2' `ppc_list1' `ppc_list2' `fte_pc_list1' `fte_pc_list2' chw* staff_target

	
	local staff_desc "No. staff"
	local doc_desc "No. doctors"
	local nurse_desc "No. nurses"
	local docnurse_desc "No. doctors and nurses"
	local midwife_desc "No. midwives"
	local medoff_desc "No. medical officers"
	local healthass_desc "No. health assistants"
	local tech_desc "No. lab tech"
	local nutr_desc "No. nutritionists"
	local soc_desc "No. social workers"
	local pharm_desc "No. pharmacists"
	local spec_desc "No. specialists"
	local manage_desc "No. management personnel"
	local techsup_desc "No. technical support"
	local mansup_desc "No. management support"
	local othermed_desc "No. other medical support"
	local othersup_desc "No. other support"
	
	foreach var in `staff_list1' `staff_list2' {
		global a`var'_desc "``var'_desc' (full and part time)"
		global a`var'pc_desc "``var'_desc'/10,000 people (full and part time)"
		global m`var'pc_desc "``var'_desc'/10,000 people (full and part time males)"
		global f`var'pc_desc "``var'_desc'/10,000 people (full and part time females)"
		global p`var'pc_desc "``var'_desc'/10,000 people (part time)"
		global fte_`var'pc_desc "``var'_desc'/10,000 people (full time)"
	}
	
	foreach var in `s_list1' `s_list2' `apc_list1' `apc_list2' `mpc_list1' `mpc_list2' {
		lab var `var' "$`var'_desc"
	}
	
	foreach var in `fpc_list1' `fpc_list2' `ppc_list1' `ppc_list2' `fte_pc_list1' `fte_pc_list2' {
		lab var `var' "$`var'_desc"
	}	
	
	
	global chw_any_desc "Has comm health worker, any"
	global chw_fp_desc "Has comm health worker, family planning"
	global chw_mat_desc "Has comm health worker, maternal"
	global chw_del_desc "Has community health worker, delivery"
	global chw_child_desc "Has comm health worker, child health"
	global chw_imm_desc "Has comm health worker, immunizations"
	global chw_sti_desc "Has comm health worker, STIs"
	global chw_mal_desc "Has comm health worker, malaria"
	global chw_tb_desc "Has comm health worker, TB"
	global chw_pmtct_desc "Has comm health worker, PMTCT"
	global chw_vct_desc "Has comm health worker, VCT"
	global chw_art_desc "Has comm health worker, ART"
	
	local chw_var1 "chw_any chw_fp chw_mat chw_del chw_child chw_imm"
	local chw_var2 "chw_sti chw_mal chw_tb chw_pmtct chw_vct chw_art"
	
	foreach var in astaff adoc anurse amidwife astaffpc adocpc anursepc amidwifepc `chw_var1' `chw_var2' staff_target {
		lab var `var' "$`var'_desc"
	}
	
	merge 1:1 facil using `temp1', nogen
	
	save `temp1', replace
	
* Delivery inputs
	use "$f_inventory", clear
	
	gen q_24hcov=(q511==1) if !mi(q511)
	global q_24hcov_desc "24 hour delivery coverage provided at facility"
	
	gen q_skill=inlist(q512,1,2) if !mi(q512)
	global q_skill_desc "Skilled provider for delivery present 24 hours"
	
	gen q_docnight=regexm(q513,"A|B|C") if !inlist(q513, " ", "")
	global q_docnight_desc "Night deliveries: doctor/A1 nurse present"
	
	gen q_docday=regexm(q514,"A|B|C") if !inlist(q514, " ", "")
	global q_docday_desc "Day deliveries: doctor/A1 nurse present"
	
	gen q_docnightday=q_docnight==1 & q_docday==1 if !mi(q_docnight) & !mi(q_docday)
	global q_docnightday_desc "Doctor/A1 nurse present for deliveries" 
	
	gen q_delmeet=q527
	recode q_delmeet (2=0)
	global q_delmeet_desc "Meetings to discuss delivery stats"
	
	gen q_delstat=regexm(q528,"A|B|X") if !inlist(q528, " ", "")
	global q_delstat_desc "Evidence of delivery stats monitoring" 
	
	gen q_advmeet=q530
	recode q_advmeet (1/3=1) (4=0)
	global q_advmeet_desc "Meetings to discuss adverse deliveries"
	
	gen q_delmonitor=1/3*q_delmeet+1/3*q_delstat+1/3*q_advmeet
	global q_delmonitor_desc "Delivery stats monitoring indicator$^\dag$"
	
	merge 1:1 facil using "$r_fac_controls", nogen
	pca q_delmeet q_delstat q_advmeet if treatment==0
	predict q_delmon_pca 
	global q_delmon_pca_desc "Delivery stats monitoring indicator$^*$"
	
	
	gen q_bedsmward=q532
	global q_bedsmward_desc "No. beds in maternity ward"
	
	gen q_bedsgward=q533
	global q_bedsgward_desc "No. delivery beds in general ward"
	
	gen q_pipedwat=(q536_01==1 | q536_01==2) if !mi(q536_01)
	global q_pipedwat_desc "Piped water in delivery room"
	
	gen q_soap=(q536_04==1 | q536_04==2) if !mi(q536_04)
	global q_soap_desc "Handsoap in delivery room"	
	
	gen q_towel=(q536_05==1 | q536_05==2) if !mi(q536_05)
	global q_towel_desc "Hand towels in delivery room"	
	
	gen q_trash=(q536_06==1 | q536_06==2) if !mi(q536_06)
	global q_trash_desc "Trash bin in delivery room"	
	
	gen q_sharps=(q536_07==1 | q536_07==2) if !mi(q536_07)
	global q_sharps_desc "Sharps bin in delivery room"	

	gen q_gloves=(q536_08==1 | q536_08==2) if !mi(q536_08)
	replace q_gloves=1 if q536_09==1 | q536_09==2
	replace q_gloves=1 if a537_09==1 | a537_09==2
	global q_gloves_desc "Any gloves in delivery room"	
	
	gen q_disinf=(q536_10==1 | q536_10==2) if !mi(q536_10)
	replace q_disinf=1 if q536_11==1 | q536_11==2
	global q_disinf_desc "Any disinfectant in delivery room"	

	gen q_needles=(q536_12==1 | q536_12==2) if !mi(q536_12)
	global q_needles_desc "Disposable needles in delivery room"	

	gen q_syringes=(q536_13==1 | q536_13==2) if !mi(q536_13)
	replace q_syringes=1 if q536_14==1 | q536_14==2
	global q_syringes_desc "Any syringes in delivery room"	
	
	gen q_privacy=(q536_15==1 | q536_15==2) if !mi(q536_15)
	global q_privacy_desc "Delivery room is private"	
	
	gen q_table=(q536_18==1 | q536_18==2) if !mi(q536_18)
	global q_table_desc "Exam table in delivery room"	
	
	local sup_list="soap towel trash sharps gloves disinf needles syringes table"
	
	gen sum_supplies=0
	
	foreach var in `sup_list' {
		replace sum_supplies=sum_supplies+q_`var'
	}

	gen q_supplies=sum_supplies==9 if !mi(sum_supplies)
	replace q_supplies=q_supplies*100
	global q_supplies_desc "Has basic delivery supplies (9 items) (x100)"
	* Missing if any item missing
	
	recode a537* (1/2=1) (3=0) (8=.)
	egen q_sup_oth=rowtotal(a537*)
	recode q_sup_oth (1/max=1)
	replace q_sup_oth=q_sup_oth*100
	global q_sup_oth_desc "Any specialized delivery supplies (11 items) (x100)"
	
	recode a538* (1/2=1) (3=0) (8=.)
	egen q_med=rowtotal(a538*)
	recode q_med (1/max=1)
	replace q_med=q_med*100
	global q_med_desc "Any delivery medications (19 items) (x100)"
	
	recode a539* (1/2=1) (3=0) (8=.)
	egen q_sup_nb=rowtotal(a539*)
	recode q_sup_nb (1/max=1)
	replace q_sup_nb=q_sup_nb*100
	global q_sup_nb_desc "Any supplies for newborn care (11 items) (x100)"

	/// WHO guidelines
	gen q_delambul=(q146==1 & q147==1) if !mi(q146)
	gen temp_ambul=(q450_2==1 & q450_4==1) if !mi(q450_2)
	replace q_delambul=1 if temp_ambul==1
	global q_delambul_desc "Emergency transport for deliveries"
	
	gen q_ambul=(q146==1 & q147==1) if !mi(q146)
	gen q_obstetambul=(q450_2==1 & q450_4==1) if !mi(q450_2)
	global q_ambul_desc "Emergency transport for any service"
	global q_obstetambul_desc "Transport for obstetric emergencies"

	gen q_light=inlist(a537_01,1,2) & b537_01==1 if !mi(a537_01)
	global q_light_desc "Examination light (x100)"
	
	egen q_delpack=rowtotal(a537_10 a537_07 a537_06 a537_08), miss
	recode q_delpack (0/3=0) (4=1)
	global q_delpack_desc "Delivery pack: (clamp, scissors, sutures, needle holder) (x100)"
	
	gen q_suct=(a539_05==1 & b539_05==1) if !mi(a539_05)
	replace q_suct=1 if (a539_06==1 & b539_06==1)
	global q_suct_desc "Suction apparatus (x100)"
	
	recode q550a (8=.)
	gen q_vac=(q550a==1 & q550b==1) if !mi(q550a)
	replace q_vac=0 if q549==2
	global q_vac_desc "Manual vacuum extractor (x100)"
	
	recode a553_1 (1/2=1) (3=0) (8=.)
	gen q_asp=(a553_1==1 & b553_1==1) if !mi(a553_1)
	replace q_asp=0 if q552==2
	global q_asp_desc "Vacuum aspirator (x100)"

	gen q_bag=(a539_01==1 & b539_01==1) if !mi(a539_01)
	global q_bag_desc "Neonatal bag and mask (x100)"
	
	recode q540* (8=.)
	gen q_proto=inlist(q540_1,1,2) if !mi(q540_1)
	global q_proto_desc "Delivery guidelines available"
	
	gen q_parto=inlist(q540_4,1,2) if !mi(q540_4)
	global q_parto_desc "Blank partographs (x100)"

	/// Medications should be checked for valid stock, but unobserved meds not checked
	/// Coded yes if reported or observed 
	gen q_eyemed=a538_09
	global q_eyemed_desc "Antibiotic eye ointment for newborns (x100)"
	
	gen q_utero=a538_03
	global q_utero_desc "Injectable uterotonic available"
	
	egen q_antibi=rowtotal(a538_07 a538_08 a538_11)
	recode q_antibi (1/max=1)
	global q_antibi_desc "Injectable antibiotic available"
	
	gen q_magne=a538_05
	global q_magne_desc "Injectable magnesium sulphate available"
	
	gen q_diaz=a538_04
	global q_diaz_desc "Injectable diazepam available"
	
	gen q_skindis=a537_03
	global q_skindis_desc "Skin disinfectant available"

	egen q_iv=rowtotal(a538_01 a537_04), missing
	recode q_iv (0/1=0) (2=1)
	global q_iv_desc "IV solution with infusion set available"
	
	mvdecode catchment, mv(9999995=.m \ 9999998=.d)
	gen cbr=39.2/1000
	/// 0.15=stillbirths, 0.7=women pregnant in given month
	gen preg_women=cbr*catchment*(1.15)*0.70 
	replace preg_women=preg_women/1000
	gen q_bedscaled=q_bedsmward/preg_women
	global q_bedscaled_desc "Maternity beds/1,000 pregnant women"
	gen q_bedstarget=q_bedscaled>=10
	global q_bedstarget_desc "Meets target for maternity beds"
	
	gen q_medwho=0
	gen q_medwho_tot=0
	
	foreach var in q_utero q_antibi q_magne q_diaz q_skindis q_iv {
		replace q_medwho=q_medwho+`var' if !mi(`var')
		replace q_medwho_tot=q_medwho_tot+1 if !mi(`var')
	}
	
	replace q_medwho=q_medwho/q_medwho_tot
	global q_medwho_desc "Obstetric medications indicator (WHO guidelines)$^\dag$"
	
	foreach var in q_light q_delpack q_suct q_vac q_asp q_bag q_parto q_eyemed { 
		recode `var' (1=100)
	}
	
	foreach var in q_sup_nb q_med q_sup_oth q_supplies q_table q_privacy q_syringes q_needles q_disin /*
		*/ q_gloves q_sharps q_trash q_towel q_soap q_pipedwat q_bedsgward /*
		*/ q_bedsmward q_docday q_docnight q_docnightday q_skill q_24hcov q_bedscaled q_bedstarget /*
		*/ q_proto q_delambul q_light q_delpack q_suct q_vac q_asp q_bag q_parto /*
		*/ q_eyemed q_utero q_antibi q_magne q_diaz q_skindis q_iv q_medwho /*
		*/ q_delmeet q_delstat q_advmeet q_delmonitor { 
		lab var `var' "$`var'_desc"
	}
	
	
	keep facil q_*
	merge 1:1 facil using `temp1', nogen
	save `temp1', replace


* Vaccine inputs
	use "$f_inventory", clear
	
	gen vac_store=(q202==1)*100 if !mi(q202) 
	
	gen fridge_func=(q204>=2 & q204<=8)*100 if !mi(q204) /* between 2 and 8 degrees */
	replace fridge_func=0 if q208==2 /* protected from sunlight */
	
	forval drug=1/5 {
		
		gen stockout_`drug'=regexm(h209_`drug',"O")*100 if !inlist(h209_`drug', " ", "","?")
		
		foreach record in i j k {

			* Use only those reporting units A (vials?)
			replace `record'209_`drug'=. if a209_`drug'!="A"

			* Rescale into 6 months
			gen `record'_`drug'=`record'209_`drug'/l209_`drug'*6
		}
	}

	rename i_1 tt_got
	rename i_2 bcg_got
	rename i_3 polio_got
	rename i_4 penta_got
	rename i_5 measles_got
	
	rename j_1 tt_disb
	rename j_2 bcg_disb
	rename j_3 polio_disb
	rename j_4 penta_disb
	rename j_5 measles_disb
	
	rename stockout_1 tt_out
	rename stockout_2 bcg_out
	rename stockout_3 polio_out
	rename stockout_4 penta_out
	rename stockout_5 measles_out
	
	keep facil tt* bcg* polio* penta* measles* vac_store fridge_func
	
	global vac_store_desc "Stores any vaccine (x100)"
	global fridge_func_desc "Functioning fridge (2-8 deg, out of sunlight) (x100)"
	global tt_got_desc "Tetanus vaccine supply (6 mo)"
	global bcg_got_desc "BCG vaccine supply (6 mo)"
	global polio_got_desc "Polio vaccine supply (6 mo)"
	global penta_got_desc "Pentavalent vaccine supply (6 mo)"
	global measles_got_desc "Measles vaccine supply (6 mo)"
	global tt_disb_desc "Tetanus vaccine disbursement (6 mo)"
	global bcg_disb_desc "BCG vaccine disbursement (6 mo)"
	global polio_disb_desc "Polio vaccine disbursement (6 mo)"
	global penta_disb_desc "Pentavalent vaccine disbursement (6 mo)"
	global measles_disb_desc "Measles vaccine disbursement (6 mo)"
	global tt_out_desc "Tetanus vaccine stockout in last 6 mo (x100)"
	global bcg_out_desc "BCG vaccine stockout in last 6 mo (x100)"
	global polio_out_desc "Polio vaccine stockout in last 6 mo (x100)"
	global penta_out_desc "Pentavalent vaccine stockout in last 6 mo (x100)"
	global measles_out_desc "Measles vaccine stockout in last 6 mo (x100)"
	
	local vac_sys="vac_store fridge_func"
	local vac_receive="tt_got bcg_got polio_got penta_got measles_got"
	local vac_disburse="tt_disb bcg_disb polio_disb penta_disb measles_disb"
	local vac_out="tt_out bcg_out polio_out penta_out measles_out"
	
	foreach var in `vac_sys' `vac_receive' `vac_disburse' `vac_out' {
		lab var `var' "$`var'_desc"
	}
	merge 1:1 facil using `temp1', nogen
	save `temp1', replace



* Child health service days of service provision 
	use "$f_inventory", clear
	
	gen days_penta=a232_1
	gen days_measles=a232_2
	gen days_bcg=a232_3
	* gen days_vita=a232_4
	gen days_sc=a232_5
	gen days_gm=a232_6
	gen days_fp=q304
	gen days_anc=q402

	gen dout_penta=b232_1
	gen dout_measles=b232_2
	gen dout_bcg=b232_3
	* gen dout_vita=b232_4
	gen dout_sc=b232_5
	gen dout_gm=b232_6
	gen dout_fp=q304a
	
	keep facil days* dout*
	
	global days_penta_desc "Penta vaccine: days per week provided"
	global days_measles_desc "Measles vaccine: days per week provided"
	global days_bcg_desc "BCG vaccine: days per week provided"
	global days_sc_desc "Sick child consults: days per week provided"
	global days_gm_desc "Growth monitoring: days per week provided"
	global days_fp_desc "Family planning: days per month provided"
	global days_anc_desc "ANC services: days per month provided"
	
	global dout_penta_desc "Penta vaccine: outreach days per month"
	global dout_measles_desc "Measles vaccine: outreach days per month"
	global dout_bcg_desc "BCG vaccine: outreach days per month"
	global dout_sc_desc "Sick child consults: outreach days per month"
	global dout_gm_desc "Growth monitoring: outreach days per month"
	global dout_fp_desc "Family planning: outreach days per month"
	
	local days="days_penta days_measles days_bcg days_sc days_gm days_fp days_anc"
	local dout="dout_penta dout_measles dout_bcg dout_sc dout_gm dout_fp"
	
	foreach var in `days' `dout' {
		lab var `var' "$`var'_desc"
	}
	
	merge 1:1 facil using `temp1', nogen
	save `temp1', replace

* Contraceptive supplies
	use "$f_inventory", clear
	
	gen contra_store=inlist(q370,1,2,3)*100 if !mi(q370)
		
	local c_list "01 02 03 04 05 06 07 08 09 10 11 12"
	
	* Define majority unit type
	local unit01="T"
	local unit02="T"
	local unit03="A"
	local unit04="A"
	local unit05="P"
	local unit06="?"
	local unit07="?"
	local unit08="?"
	local unit09="?"
	local unit10="?"
	local unit11="?"
	local unit12="?"	

	foreach contra in `c_list' {
		
		gen stockout_`contra'=regexm(h372_`contra',"O")*100 if !inlist(h372_`contra', " ", "","?")
		
		
		foreach record in i j {

			* Use only those reporting units A (vials?)
			replace `record'372_`contra'=. if a372_`contra'!="`unit`contra''"
			
			* Rescale into 6 months
			gen `record'_`contra'=`record'372_`contra'/l372_`contra'*6
		}
	}
	
	rename i_01 cpill_got
	rename j_01 cpill_disb
	rename stockout_01 cpill_out
	rename i_02 ppill_got
	rename j_02 ppill_disb
	rename stockout_02 ppill_out
	rename i_03 cinject_got
	rename j_03 cinject_disb
	rename stockout_03 cinject_out
	rename i_04 pinject_got
	rename j_04 pinject_disb
	rename stockout_04 pinject_out
	rename i_05 mcondom_got
	rename j_05 mcondom_disb
	rename stockout_05 mcondom_out
	rename i_06 fcondom_got
	rename j_06 fcondom_disb
	rename stockout_06 fcondom_out
	rename i_07 iud_got
	rename j_07 iud_disb
	rename stockout_07 iud_out
	rename i_08 implant_got
	rename j_08 implant_disb
	rename stockout_08 implant_out
	rename i_09 sperm_got
	rename j_09 sperm_disb
	rename stockout_09 sperm_out
	rename i_10 diaph_got
	rename j_10 diaph_disb
	rename stockout_10 diaph_out
	rename i_11 emer_got
	rename j_11 emer_disb
	rename stockout_11 emer_out
	rename i_12 beads_got
	rename j_12 beads_disb
	rename stockout_12 beads_out
	
	* Dropping cinject, spermicide, diaphragms, emergency, female condoms
	* iud, implants, beads: two few observations
	
	keep facil cpill* ppill* pinject* mcondom* contra_store
	
	global contra_store_desc "Stores any contraceptives (x100)"
	global cpill_out_desc "Combined pill stockout in last 6 mo (x100)"
	global cpill_got_desc "Combined pill supply (6 mo)"
	global cpill_disb_desc "Combined pill disbursement (6 mo)"
	global ppill_out_desc "Progestin pill stockout in last 6 mo (x100)"
	global ppill_got_desc "Progestin pill supply (6 mo)"
	global ppill_disb_desc "Progestin pill disbursement (6 mo)"
	global pinject_out_desc "Progestin injection stockout in last 6 mo (x100)"
	global pinject_got_desc "Progestin injection supply (6 mo)"
	global pinject_disb_desc "Progestin injection disbursement (6 mo)"
	global mcondom_out_desc "Male condom stockout in last 6 mo (x100)"
	global mcondom_got_desc "Male condom supply (6 mo)"
	global mcondom_disb_desc "Male condom disbursement (6 mo)"
	
	local contra1="contra_store cpill_out cpill_got cpill_disb"
	local contra2="ppill_out ppill_got ppill_disb pinject_out pinject_got pinject_disb"
	local contra3="mcondom_out mcondom_got mcondom_disb"
	
	foreach var in `contra1' `contra2' `contra3' {
		lab var `var' "$`var'_desc"
	}
	
	merge 1:1 facil using `temp1', nogen
	save `temp1', replace
	
** HIV VCT
	use "$f_vct", clear
	
	bysort facil: egen days_vct=mean(q1804)
	bysort facil: egen days_test=mean(q1818)
	
	gen temp=regexm(q1825,"A")*100 if !inlist(q1825, " ", "")
	bysort facil: egen inclinic_rapid=mean(temp)
	
	bysort facil: keep if _n==1
	
	keep facil days_vct days_test inclinic_rapid
	
	global days_vct_desc "Days per week counseling available (mean across clinics)"
	global days_test_desc "Days per week HIV testing available (mean across clinics)"
	global inclinic_rapid_desc "Rapid testing in clinic (x100) (mean across clinics)"
	
	foreach var in days_vct days_test inclinic_rapid {
		lab var `var' "$`var'_desc"
	}
	
	merge 1:1 facil using `temp1', nogen
	save `temp1', replace
	
** Laboratory records
	use "$f_lab", clear
	merge m:1 facil using "$r_fac_controls", keep(3) nogen
	
	gen test_cd4=inlist(b1511_2, 1,2)*100 if !mi(a1511_2)
	gen test_rapid=inlist(b1511_4, 1,2)*100 if !mi(a1511_4)
	gen hiv_test=b1513_1/m1513_1*12/catchment*100
	gen hiv_pos=b1513_2/m1513_2*12/catchment*100 
	
	* Collapse to facility mean for single facility with multiple labs
	foreach var in test_cd4 test_rapid hiv_test {
			bysort facil: egen temp=mean(`var')
			replace `var'=temp
			drop temp
	}
	
	foreach var in hiv_test hiv_pos {
			bysort facil: egen temp=total(`var')
			replace `var'=temp
			drop temp
	}

	global test_cd4_desc "Functioning equipment for cd4 count test (x100)"
	global test_rapid_desc "Functioning equipment for rapid HIV test (x100)"
	global hiv_test_desc "No. clients getting HIV tests/catchment (lab records) (x100)"
	global hiv_pos_desc "No. clients testing HIV+/catchment (lab records) (x100)"
	
	foreach var in test_cd4 test_rapid hiv_test hiv_pos {
		lab var `var' "$`var'_desc"
	}
	
	bysort facil: keep if _n==1
	keep facil test_cd4 test_rapid hiv_test hiv_pos
	
	merge 1:1 facil using `temp1', nogen
	save "$d_interm\inputs", replace
	
** Health staff
	use "$f_prov", clear
	
	gen hr_worked=h800
	global hr_worked_desc "Average hours/week worked in facility$^\dag$"
	
	gen h_schoolyrs=h104
	global h_schoolyrs_desc "Years of schooling"
	
	gen h_doctor=inlist(h105,1,2,3,4,5) if !mi(h105)
	global h_doctor_desc "Doctor"
	
	gen h_nurse=inlist(h105,8,9,10) if !mi(h105)
	global h_nurse_desc "Nurse"
	
	gen h_age=h109
	global h_age_desc "Age"
	
	recode h203a* (2=0)
	
	gen h_sti=h203a_1==1 if !mi(h203a_1)
	global h_sti_desc "Provides STI services"
	
	gen h_malaria=h203a_2==1 if !mi(h203a_2)
	global h_malaria_desc "Provides malaria services"

	egen h_tb=rowtotal(h203a_3 h203a_4 h203a_5 h203a_6 h203a_7 h203a_8), m
	recode h_tb (1/max=1)
	global h_tb_desc "Provides TB services"

	gen h_sc=(h301==1) if !mi(h301)
	global h_sc_desc "Provides child health services"
	
	gen h_fp=(h401==1) if !mi(h401)
	global h_fp_desc "Provides family planning services"
	
	gen h_mhealth=(h501==1) if !mi(h501)
	global h_mhealth_desc "Provides maternal health services"

	recode h601a* (2=0)
	egen h_hiv=rowtotal(h601a*), m 
	recode h_hiv (1/max=1)
	global h_hiv_desc "Provides HIV/AIDS services"

	gen h_lab=(h701==1) if !mi(h701)
	global h_lab_desc "Provides laboratory services"
	
	foreach var in hr_worked h_schoolyrs h_doctor h_nurse h_age /*
		*/ h_sti h_malaria h_tb h_sc h_fp h_mhealth h_hiv h_lab {
		lab var `var' "$`var'_desc"
	}
	
	keep facil pline hr_worked h_*
	save "$d_interm\staff", replace

***********************************************
* (0.2) Combine into indicators 
* 		(includes various methods of weighting)
***********************************************
	
	use "$f_inventory", clear
	
	gen d_priv = inlist(q534,1) if !mi(q534)
	global d_priv_desc "Private delivery room (auditory and visual)"
	
	gen d_table= inlist(q536_18,1,2) if !mi(q536_18)
	global d_table_desc "Exam table available"
	
	gen d_instr1= inlist(a537_07,1,2) if !mi(a537_07)
	gen d_instr2= inlist(a537_08,1,2) if !mi(a538_07)
	gen d_instr=d_instr1+d_instr2
	recode d_instr (1=0) (2=1)
	global d_instr_desc "Sterilized instruments available"
	
	gen d_asp = inlist(b539_05,1) if !mi(b539_05)
	replace d_asp=0 if inlist(a539_05,3,8)
	replace d_asp = 1 if inlist(b539_06,1)
	global d_asp_desc "Neonatal aspirator available"
	
	gen d_steth = inlist(b439_04,1) if !mi(b439_04)
	replace d_steth = 0 if inlist(a439_04,3,8)
	global d_steth_desc "Obstetrical stethoscope available"

	gen d_thread = inlist(a537_06,1,2) if !mi(a537_06)
	global d_thread_desc "Suture thread available"
	
	gen d_light = inlist(b537_01,1) if !mi(b537_01)
	replace d_light=0 if inlist(a537_01,3,8)
	global d_light_desc "Exam light available"
	
	gen d_iscale = inlist(b539_04,1) if !mi(b539_04)
	replace d_iscale=0 if inlist(a539_04,3,8)
	global d_iscale_desc "Infant scale available"
	
	gen d_edrops = inlist(b538_09,1) if !mi(b538_09)
	replace d_edrops=0 if inlist(a538_09,3,8)
	global d_edrops_desc "Ophthalmic ointment available"
	
	gen d_anesth = inlist(b538_16,1) if !mi(b538_16)
	replace d_anesth=0 if inlist(a538_16,3,8)
	global d_anesth_desc "Local anesthesia available"

	gen d_gloves=inlist(q536_08,1,2) if !mi(q536_08)
	replace d_gloves=1 if inlist(q536_09,1,2) 
	replace d_gloves=1 if inlist(a537_09,1,2)
	global d_gloves_desc "Sterile gloves available"	

	gen d_clamp = inlist(a537_10,1,2) if !mi(a537_10)
	global d_clamp_desc "Umbilical cord clamp available"
	
	gen d_equip=0
	gen d_equip_tot=0
	
	foreach var in d_table d_instr d_asp d_steth d_thread d_light d_iscale d_edrops d_anesth d_gloves d_clamp {
		lab var `var' "$`var'_desc"
		replace d_equip = d_equip+`var' if !mi(`var')
		replace d_equip_tot = d_equip_tot + 1 if !mi(`var')
	}
	
	replace d_equip = d_equip/d_equip_tot
	
	/// Score 0 if any of first five are missing
	gen d_mand=0
	gen d_mand_tot=0
	
	foreach var in d_table d_instr d_asp d_steth d_thread {
		replace d_mand = d_mand+`var' if !mi(`var')
		replace d_mand_tot = d_mand_tot + 1 if !mi(`var')	
	}
	
	replace d_mand = d_mand/d_mand_tot
	
	replace d_equip = 0 if (d_mand!=1 & d_mand!=.)
	global d_equip_desc "Delivery room inputs indicator (P4P multiplier)$^\dag$"
	
	gen d_equip_w = 18
	gen d_priv_w=7
	
	keep facil d_* 
		
	gen del_mult=0
	gen del_weight=0
	
	foreach var in d_priv d_equip {
		lab var `var' "$`var'_desc"
		replace `var'_w = . if `var'==.
		replace del_mult=del_mult+`var'*`var'_w if !mi(`var') & !mi(`var'_w)
		replace del_weight=del_weight+`var'_w if !mi(`var'_w)
	}
	
	replace del_mult=del_mult/del_weight
	
	/// Drop observations that do not have all items 
	replace del_mult=. if del_weight<25
	
	global del_mult_desc "Overall delivery indicator (P4P multiplier)"
	lab var del_mult "$del_mult_desc"

	merge 1:1 facil using "$d_interm\inputs", nogen
	
	
	gen q_phys=0
	gen q_phys_tot=0
	
	local equip_list1="d_table d_instr d_asp d_steth d_thread d_light d_iscale"
	local equip_list2="d_edrops d_anesth d_gloves d_clamp"
	local med_list="q_utero q_antibi q_magne q_diaz q_skindis q_iv"
	
	foreach var in `equip_list1' `equip_list2' `med_list' {
		replace q_phys = q_phys+`var' if !mi(`var')
		replace q_phys_tot = q_phys_tot + 1 if !mi(`var')
	}
	
	* Del equipment and medication is mean of items with data
	replace q_phys=q_phys/q_phys_tot
	replace q_phys=. if q_phys_tot<15 
	/*missing variable if missing data for more than 2 of 17 items */ 

	
	global q_phys_desc "Delivery equipment and medication indicator$^\dag$"
	lab var q_phys "$q_phys_desc"
	
	merge 1:1 facil using "$r_fac_controls", nogen
	pca `equip_list1' `equip_list2' `med_list' if treatment==0
	predict q_phys_pca
	global q_phys_pca_desc "Delivery equipment and medication indicator$^*$"
	
foreach var in q_phys_pca q_delmon_pca {
	egen max_`var'=max(`var')
	egen min_`var'=min(`var')
	* Normalizing to be fraction of achievement
	replace `var'=(`var'-min_`var')/(max_`var'-min_`var')
}

drop max* min*

save "$d_interm\del_mult", replace

